public class Trip {
	private int mNumberOfStudents;
	private int mLastIndex;
	private double[] mMoneyList;

	public Trip(int numberOfStudents) {
		mNumberOfStudents = numberOfStudents;
		mMoneyList = new double[numberOfStudents];
		mLastIndex = 0;
	}

	public int getNumberOfStudents() {		
		return mNumberOfStudents;
	}

	public void addMoney(double money) throws Exception {
		if(mLastIndex + 1 > mNumberOfStudents) throw new Exception();
		mMoneyList[mLastIndex] = money;
		++mLastIndex;
	}

	public double[] getMoneyList() {		
		return mMoneyList;
	}

	public double getMeanSpiltMoney() throws Exception {		
		return calculateMeanSpiltMoney();
	}

	private double calculateMeanSpiltMoney(){		
		double result_upper = 0;
		double result_lower = 0;

		double mean = calculateMean(mMoneyList);		

		for(int i = 0; i < mMoneyList.length; ++i) {
			if(mMoneyList[i] > mean)
				result_upper += mMoneyList[i] - mean;
			else
				result_lower += mean - mMoneyList[i];					
		}				
		/*
		double total = calculateTotal(mMoneyList);
		double remain = total - (mean * mMoneyList.size());
		remain = rounding(remain, 2);		
		if(remain != 0.0) {
			result -= (remain / 2);
		}
		 */

		if(result_lower > result_upper)
			return result_upper;
		else
			return result_lower;

	}

	static public double calculateMean(double[] moneyList) {
		double result = 0;

		double total = calculateTotal(moneyList);		
		result = total / moneyList.length;

		return ((int)result * 100) / 100;
	}

	static public double calculateTotal(double[] moneyList) {
		double total = 0;
		for(int i = 0; i < moneyList.length; ++i) {
			total += moneyList[i];
		}

		return rounding(total, 2);
	}

	static public double rounding(double number, int lastpoint) {
		long result = Math.round(number * 100.00);				
		return result / 100.00;
	}

}
